package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.utils.common.AppThreadLocalUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-04
 */
@Service
@Slf4j
@AllArgsConstructor
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements IApUserFollowService {

    private final ApUserFollowMapper apUserFollowMapper;
    private final ApUserFanMapper apUserFanMapper;



    @Override
    public ResponseResult followOrUnfollow(UserRelationDto dto) {
        if (dto.getAuthorId() == Long.valueOf(AppThreadLocalUtil.getUser()+"")){
            return ResponseResult.errorResult(AppHttpCodeEnum.FOllOW_MYSELF);
        }
        //判断是否关注
        switch(dto.getOperation()){
            case 0:follow(dto);
            break;
            case 1:unfollow(dto);
            break;
        }
        return ResponseResult.okResult("");
    }

    /**
     * 查询是否关注该文章作者
     * @param dto
     * @return
     */
    @Override
    public Boolean isFollow(UserRelationDto dto) {
        LambdaQueryWrapper<ApUserFollow> wrapper =  new LambdaQueryWrapper<>();
        wrapper.eq(ApUserFollow::getFollowId,dto.getAuthorId());
        wrapper.eq(ApUserFollow::getUserId,AppThreadLocalUtil.getUser());
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(wrapper);
        return ObjectUtils.isNotEmpty(apUserFollow);
    }

    @Transactional
    public void unfollow(UserRelationDto dto) {
        LambdaQueryWrapper<ApUserFollow> wrapper =  new LambdaQueryWrapper<>();
        wrapper.eq(ApUserFollow::getUserId,AppThreadLocalUtil.getUser());
        apUserFollowMapper.delete(wrapper);
        LambdaQueryWrapper<ApUserFan> dwrapper =  new LambdaQueryWrapper<>();
        dwrapper.eq(ApUserFan::getUserId,Integer.parseInt(dto.getAuthorId() + ""));
        dwrapper.eq(ApUserFan::getFansId,AppThreadLocalUtil.getUser());
        apUserFanMapper.delete(dwrapper);
    }

    @Transactional
    public void follow(UserRelationDto dto) {
        ApUserFollow apUserFollow = new ApUserFollow();
        apUserFollow.setUserId(AppThreadLocalUtil.getUser());
        apUserFollow.setFollowId(Integer.parseInt(dto.getAuthorId() + ""));
        apUserFollowMapper.insert(apUserFollow);
        //存储用户粉丝表
        ApUserFan apUserFan = new ApUserFan();
        apUserFan.setUserId(Integer.parseInt(dto.getAuthorId() + ""));
        apUserFan.setFansId(AppThreadLocalUtil.getUser());
        apUserFanMapper.insert(apUserFan);
    }
}